package com.example.demo.specifications;

import com.example.demo.entity.Message;
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;


public class MessageSpecifications {

    public static Specification<Message> conversationBetweenUsers(Long userId1, Long userId2) {
        return (root, query, criteriaBuilder) -> {
            Predicate predicate1 = criteriaBuilder.and(
                    criteriaBuilder.equal(root.get("senderId"), userId1),
                    criteriaBuilder.equal(root.get("receiverId"), userId2)
            );
            Predicate predicate2 = criteriaBuilder.and(
                    criteriaBuilder.equal(root.get("senderId"), userId2),
                    criteriaBuilder.equal(root.get("receiverId"), userId1)
            );
            return criteriaBuilder.or(predicate1, predicate2);
        };
    }
}